package com.play.parse.impl;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.play.parse.JsoupHandler;

import edu.uci.ics.crawler4j.parser.ExtractedUrlAnchorPair;

public class M18Handler extends JsoupHandler{
	protected static final Logger logger = Logger.getLogger(M18Handler.class.getName()); 
	
	/**
	 * 商品网
	 */
	protected void productSeed(Document doc) {
		Elements links = doc.select("div.section_prdsort").select("a[href]");
		if(null != links){
			for (Element element : links) {
				ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
				String seedurl = element.attr("abs:href");
				curUrl.setHref(seedurl);
				outgoingurls.add(curUrl);
			}
		}
	}
	
	/**
	 * 麦考林可以的detail页面来源直接获取价格
	 */
	protected void productList(Document doc) {
		Elements links = doc.select("div#search_result_item_list");
		
		if(null != links && 0 != links.size()) {
			for (Element src : links) {
				Elements href = src.select("a");
				for(Element price : href) {
					String link = price.attr("abs:href");
					if (StringUtils.isBlank(link)) {
						continue;
					}
					ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
					curUrl.setHref(link);
					outgoingurls.add(curUrl);
					//System.out.println(link);
				}
			}
		}
		
		//获取分页信息
		Elements pages = doc.select("div#pageing_list a");
		for (Element page : pages) {
			String pageUrl = page.attr("abs:href");
			//logger.info(pageUrl);
			ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
			curUrl.setHref(pageUrl);
			outgoingurls.add(curUrl);
		}
	}

	protected void productInfo(String contextURL, Document doc) {
		Elements info = doc.select("span#ProcessBtn_direct,span#ProcessBtn_cart");
		if(info != null) {
			
			StringBuilder prodect = new StringBuilder(contextURL);
			prodect.append(" ~ ");
			
			//价格
			Elements price = doc.select("dd > strong[data-price]");
			if(price==null || price.size()==0)
				return;
			prodect.append(price.first().text()).append(" ~ ");
			
			//获取位置（类别路径）信息
			//Elements path = doc.select("div.tm_now,div.this_page,div[class~=tm_now]");
			Elements path = doc.select("dl.category").select("a.menuName");
			prodect.append(path.text()).append(" ~ ");
			
			//产品名称，不同类别不一样
			//Elements name = doc.select("div.p_info h2,div.p_info h1");
			Elements name = doc.getElementsByTag("title");
			prodect.append(name.text()).append(" ~ ");
			//获取产品信息
			Elements atrr = doc.select("div.contentWrap");
			prodect.append(atrr.text());
			prodect.append("\n");
			//logger.info(info.select(".p-price").text());
			try {
				FileUtils.write(new File(filePath+File.separator+getClass().getSimpleName()+File.separator+"productInfo.txt"), prodect, true);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	protected String getFilePath() {
		return  "crawlerdata";
	}
	
}
